<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('interviewGroup.fields.groupName')" prop="groupName">
        <el-input
          v-model="formData.groupName"
          :placeholder="t('common.inputTextPlaceholder') + t('interviewGroup.fields.groupName')"
        />
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.meetingNumber')" prop="meetingNumber">
        <el-input
          v-model="formData.meetingNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('interviewGroup.fields.meetingNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.meetingPassword')" prop="meetingPassword">
        <el-input
          v-model="formData.meetingPassword"
          :placeholder="
            t('common.inputTextPlaceholder') + t('interviewGroup.fields.meetingPassword')
          "
        />
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.qqGroupNumber')" prop="qqGroupNumber">
        <el-input
          v-model="formData.qqGroupNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('interviewGroup.fields.qqGroupNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.specialistId1')" prop="specialistId1">
        <el-select
          v-model="formData.specialistId1"
          :placeholder="
            t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId1')
          "
          clearable
        >
          <el-option
            v-for="(item, index) in specialistList"
            :key="index"
            :label="item.nickname"
            :value="item.userId"
            :disabled="
              item.userId === formData.specialistId2 || item.userId === formData.specialistId3
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.specialistId2')" prop="specialistId2">
        <el-select
          v-model="formData.specialistId2"
          :placeholder="
            t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId2')
          "
          clearable
        >
          <el-option
            v-for="(item, index) in specialistList"
            :key="index"
            :label="item.nickname"
            :value="item.userId"
            :disabled="
              item.userId === formData.specialistId1 || item.userId === formData.specialistId3
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('interviewGroup.fields.specialistId3')" prop="specialistId3">
        <el-select
          v-model="formData.specialistId3"
          :placeholder="
            t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId3')
          "
          clearable
        >
          <el-option
            v-for="(item, index) in specialistList"
            :key="index"
            :label="item.nickname"
            :value="item.userId"
            :disabled="
              item.userId === formData.specialistId1 || item.userId === formData.specialistId2
            "
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleNext" type="primary" :disabled="formLoading"
        >{{ t('common.nextLabel') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { InterviewGroupApi, InterviewGroupVO } from '@/api/recruit/interviewgroup'
import { FormRules } from 'element-plus'
import { InterviewVO } from '@/api/recruit/interview'
import { BatchSpecialistApi, BatchSpecialistSimpleListRespVO } from '@/api/recruit/batchspecialist'
import { delay } from '@/utils'

/** 面试分组 表单 */
defineOptions({ name: 'InterviewGroupForm' })

const props = defineProps<{
  // 接收面试信息 用来挂计划ID和面试ID
  interviewInfo: InterviewVO
}>()

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  batchId: undefined,
  interviewId: undefined,
  groupName: undefined,
  meetingNumber: undefined,
  meetingPassword: undefined,
  qqGroupNumber: undefined,
  specialistId1: undefined,
  specialistId2: undefined,
  specialistId3: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  groupName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('interviewGroup.fields.groupName'),
      trigger: 'blur'
    }
  ],
  meetingNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('interviewGroup.fields.meetingNumber'),
      trigger: 'blur'
    }
  ],
  meetingPassword: [
    {
      required: false,
      message: t('common.inputTextPlaceholder') + t('interviewGroup.fields.meetingPassword'),
      trigger: 'blur'
    }
  ],
  qqGroupNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('interviewGroup.fields.qqGroupNumber'),
      trigger: 'blur'
    }
  ],
  specialistId1: [
    {
      required: true,
      message: t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId1'),
      trigger: 'change'
    }
  ],
  specialistId2: [
    {
      required: true,
      message: t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId2'),
      trigger: 'change'
    }
  ],
  specialistId3: [
    {
      required: true,
      message: t('common.selectTextPlaceholder') + t('interviewGroup.fields.specialistId3'),
      trigger: 'change'
    }
  ]
})
const formRef = ref() // 表单 Ref

// 专家下拉列表
const specialistList = ref<BatchSpecialistSimpleListRespVO[]>([])

/** 打开弹窗 */
const open = async (type: string, id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await InterviewGroupApi.getInterviewGroup(id)
    } finally {
      formLoading.value = false
    }
  } else {
    // 新增时，设置默认值
    formData.value.batchId = props.interviewInfo.batchId as any
    formData.value.interviewId = props.interviewInfo.id as any
  }

  if (props.interviewInfo.batchId) {
    specialistList.value = await BatchSpecialistApi.getBatchSpecialistSimpleList(
      props.interviewInfo.batchId
    )
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emits = defineEmits<{
  (e: 'next', value: InterviewGroupVO): void
}>()

const handleNext = async () => {
  // 校验表单
  await formRef.value.validate()
  // 这里的更新group不要了 统一在最后更新大VO
  // if (formData.value.id) {
  //   // 有ID的是修改操作 调更新接口先保存一下数据
  //   await InterviewGroupApi.updateInterviewGroup(formData.value as InterviewGroupVO)
  //   message.success(t('common.updateSuccess'))
  // }
  // 发送事件
  emits('next', formData.value as InterviewGroupVO)
  await delay(1000)
  // 关闭弹窗
  dialogVisible.value = false
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    batchId: undefined,
    interviewId: undefined,
    groupName: undefined,
    meetingNumber: undefined,
    meetingPassword: undefined,
    qqGroupNumber: undefined,
    specialistId1: undefined,
    specialistId2: undefined,
    specialistId3: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
